﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CpuSimulator
{
    /// <summary>
    /// 表示Cpu中的一个32位通用寄存器
    /// </summary>
    public class Register
    {
        /// <summary>
        /// 寄存器中的内容，因为模拟32位系统，每个寄存器能保存一个32位的数据，所以用Int32
        /// </summary>
        public Int32 Content { get; set; }
        /// <summary>
        /// 寄存器编号，RISC-V有32个通用寄存器，编号0-31
        /// </summary>
        public short Index { get; set; }
        /// <summary>
        /// 寄存器别名，RISC-V中ABI中定义的别名
        /// </summary>
        public string Name { get; set; }

        private Int32 _value;
        /// <summary>
        /// 寄存器内部的值
        /// </summary>
        public Int32 Value => _value;

        public Register(short index, string name)
        {
            Content = 0;
            Index = index;
            Name = name;
        }

        public void SetValue(Int32 value)
        {
            //x0寄存器永远是0，不会改变
            if (Index != 0)
            {
                this._value = value;
            }
        }
        public void Clear()
        {
            if (this.Name == "PC")
            {
                this._value = Cpu.PCStartValue;
            }
            else
            {
                this._value = 0;
            }
        }
    }
}
